package com.jml.SimpleTomcat;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;


public class HttpResponse {

	private OutputStream out = null;

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


	public HttpResponse(OutputStream out) {
		super();
		this.out = out;
	}


	//这个方法的作用是把请求的地址传递进来，服务器这个时候向客户端回写响应的文件数据等
	public void writeFile( String path ) throws IOException{


		String basepath = new File("").getCanonicalPath();

		FileInputStream fis = new FileInputStream( basepath +"/"+ path);
		byte[] buff = new byte[1024];
		int len = 0;
		StringBuffer sb = new StringBuffer();
		//拼凑响应头信息
		sb.append("HTTP/1.1 200 OK\n");
		//sb.append("Content-Type: text/html;charset=UTF-8\n");
		sb.append("\r\n");


		//响应业务,
		out.write(sb.toString().getBytes());

		while((len=fis.read(buff))!=-1){
			out.write(buff,0,len);
		}

		fis.close();
		out.flush();
		out.close();

	}



}

